import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lcapp/api/interface/home_api.dart';
import 'package:lcapp/api/request/response_data.dart';
import 'package:lcapp/data/lc_video_data_entity.dart';
import 'package:lcapp/model/refresh/refresh.dart';
import 'package:lcapp/page/search/video_item.dart';

class SearchVideoPage extends StatefulWidget {
  final String searchValue;
  const SearchVideoPage({Key? key, required this.searchValue}) : super(key: key);

  @override
  _SearchVideoPageState createState() => _SearchVideoPageState();
}

class _SearchVideoPageState extends State<SearchVideoPage> {

  int pageNum = 1;
  int pageSize = 20;
  List<LcVideoDataEntity> lcVideos = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getVideos();
  }

  getVideos()async{
    ResponseData res = await HomeAPI.searchVideos(pageNum,pageSize,widget.searchValue);
    if(res.code==200){
      List arr = res.data??[];
      if(pageNum==1){
        lcVideos.clear();
      }
      for (var element in arr) {
        LcVideoDataEntity entity = LcVideoDataEntity().fromJson(element);
        lcVideos.add(entity);
      }
    }
    if(mounted){setState(() {
    });}
  }

  @override
  void didUpdateWidget(covariant SearchVideoPage oldWidget) {
    // TODO: implement didUpdateWidget
    super.didUpdateWidget(oldWidget);
    if(oldWidget.searchValue != widget.searchValue){
      pageNum = 1;
      getVideos();
    }
  }

  @override
  Widget build(BuildContext context) {
    return EasyRefresh(
      footer: myRefreshFooter(),
      header: myRefreshHeader(),
      onRefresh: ()async{
        pageNum=1;
        getVideos();
      },
      onLoad: ()async{
        pageNum++;
        getVideos();
      },
      child: GridView.builder(
          padding: EdgeInsets.zero,
          gridDelegate:
          SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 2,
              mainAxisSpacing: 11.w,
              crossAxisSpacing: 11.w,
              childAspectRatio: 0.8
          ),
          shrinkWrap: true,
          itemCount: lcVideos.length,
          itemBuilder: (BuildContext context, int i) {
            return VideoItem(entity: lcVideos[i],cliclFun: (){
              Navigator.of(context).pushNamed("MinVideoDetailPage",arguments: {
                'lcVideos':lcVideos,
                'isMine':false,
                'index':i
              });
            },);
          }),
    );
  }


}
